package top.yaofengqiao.springcloudsimple.payment.biz.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.model.entity.PaymentOrderEntity;
import top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.model.valobj.PaymentOrderStatus;
import top.yaofengqiao.springcloudsimple.payment.biz.infrastructure.dao.PaymentOrderMapper;
import top.yaofengqiao.springcloudsimple.payment.biz.infrastructure.po.PaymentOrderPO;
import top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.repository.IPaymentOrderRepository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yfq
 * @date 2024/6/14 16:59
 * @description
 */
@Repository
public class PaymentOrderRepository implements IPaymentOrderRepository {
    @Resource
    private PaymentOrderMapper paymentOrderMapper;

    @Override
    public long save(PaymentOrderEntity paymentOrderEntity) {
        PaymentOrderPO paymentOrderPO = buildPaymentOrderPO(paymentOrderEntity);
        if (paymentOrderPO == null) return -1;

        int insert = paymentOrderMapper.insert(paymentOrderPO);

        return insert == 1 ? paymentOrderPO.getId() : -1;
    }

    @Override
    public PaymentOrderEntity queryByRechargeOrderNo(String orderNo) {
        if (StrUtil.isEmpty(orderNo)) return null;

        PaymentOrderPO paymentOrderPO = paymentOrderMapper.selectOne(Wrappers.<PaymentOrderPO>lambdaQuery().eq(PaymentOrderPO::getRechargeOrderNo, orderNo));

        return buildPaymentOrderEntity(paymentOrderPO);
    }

    @Override
    public PaymentOrderEntity queryByPayOrderNo(String payOrderNo) {
        if (StrUtil.isEmpty(payOrderNo)) return null;

        PaymentOrderPO paymentOrderPO = paymentOrderMapper.selectOne(Wrappers.<PaymentOrderPO>lambdaQuery().eq(PaymentOrderPO::getPayOrderNo, payOrderNo));

        return buildPaymentOrderEntity(paymentOrderPO);
    }

    @Override
    public void orderTrading(long orderId, String content) {
        paymentOrderMapper.orderTrading(orderId, content);
    }

    @Override
    public boolean updatePaymentOrderByPayOrderNo(PaymentOrderEntity paymentOrderEntity) {
        PaymentOrderPO paymentOrderPO = buildPaymentOrderPO(paymentOrderEntity);
        if (paymentOrderPO == null) return false;

        int updated = paymentOrderMapper.update(paymentOrderPO, Wrappers.<PaymentOrderPO>lambdaUpdate()
                .eq(PaymentOrderPO::getPayOrderNo, paymentOrderPO.getPayOrderNo()));

        return updated == 1;
    }

    @Override
    public void changeStatus(String rechargeOrderNo, PaymentOrderStatus origin, PaymentOrderStatus target) {
        paymentOrderMapper.changeStatus(rechargeOrderNo, origin.getStatus(), target.getStatus());
    }

    @Override
    public List<PaymentOrderEntity> queryUnPaymentOrder(long startId, int limit) {
        List<PaymentOrderPO> paymentOrderPOS = paymentOrderMapper.queryUnPaymentOrder(startId, limit);

        return paymentOrderPOS.stream().map(this::buildPaymentOrderEntity).collect(Collectors.toList());
    }

    private PaymentOrderPO buildPaymentOrderPO(PaymentOrderEntity paymentOrderEntity) {
        if (paymentOrderEntity == null) return null;

        PaymentOrderPO paymentOrderPO = new PaymentOrderPO();
        paymentOrderPO.setId(paymentOrderEntity.getId());
        paymentOrderPO.setPayOrderNo(paymentOrderEntity.getPayOrderNo());
        paymentOrderPO.setRechargeOrderNo(paymentOrderEntity.getRechargeOrderNo());
        paymentOrderPO.setUserId(paymentOrderEntity.getUserId());
        paymentOrderPO.setPayChannel(paymentOrderEntity.getPayChannel());
        paymentOrderPO.setPayType(paymentOrderEntity.getPayType());
        paymentOrderPO.setChannelOrderNo(paymentOrderEntity.getChannelOrderNo());
        paymentOrderPO.setMerchantCode(paymentOrderEntity.getMerchantCode());
        paymentOrderPO.setPayAmount(paymentOrderEntity.getPayAmount());
        paymentOrderPO.setTransactionTime(paymentOrderEntity.getTransactionTime());
        paymentOrderPO.setStatus(paymentOrderEntity.getStatus());
        paymentOrderPO.setPayTime(paymentOrderEntity.getPayTime());
        paymentOrderPO.setRemark(paymentOrderEntity.getRemark());
        paymentOrderPO.setContent(paymentOrderEntity.getContent());
        paymentOrderPO.setCreateTime(paymentOrderEntity.getCreateTime());
        paymentOrderPO.setUpdateTime(paymentOrderEntity.getUpdateTime());

        return paymentOrderPO;
    }

    private PaymentOrderEntity buildPaymentOrderEntity(PaymentOrderPO paymentOrderPO) {
        if (paymentOrderPO == null) return null;

        return PaymentOrderEntity.builder()
                .id(paymentOrderPO.getId())
                .payOrderNo(paymentOrderPO.getPayOrderNo())
                .rechargeOrderNo(paymentOrderPO.getRechargeOrderNo())
                .userId(paymentOrderPO.getUserId())
                .payChannel(paymentOrderPO.getPayChannel())
                .payType(paymentOrderPO.getPayType())
                .channelOrderNo(paymentOrderPO.getChannelOrderNo())
                .merchantCode(paymentOrderPO.getMerchantCode())
                .payAmount(paymentOrderPO.getPayAmount())
                .transactionTime(paymentOrderPO.getTransactionTime())
                .status(paymentOrderPO.getStatus())
                .payTime(paymentOrderPO.getPayTime())
                .remark(paymentOrderPO.getRemark())
                .content(paymentOrderPO.getContent())
                .createTime(paymentOrderPO.getCreateTime())
                .updateTime(paymentOrderPO.getUpdateTime())
                .build();

    }
}
